//
//  NetReachability.m
//  MyApplication
//
//  Created by 彭凯敏 on 15/8/3.
//  Copyright (c) 2015年 52xiaoluo. All rights reserved.
//

#import "NetReachability.h"

@implementation NetReachability
{
    Reachability * localWiFiReach;
    Reachability * internetConnectionReach;

    
}

static NetReachability* _instance = nil;

+(instancetype) shareInstance
{
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        _instance = [[NetReachability alloc] init] ;
        
        
    });
    return _instance ;
}
- (id)init
{
    self.oldStatus = self.currentStatus;
    
    return self;
}
- (void)registerReach
{
    
    internetConnectionReach = [Reachability reachabilityForInternetConnection];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reachabilityChanged:)
                                                 name:kReachabilityChangedNotification
                                               object:nil];
    
    [internetConnectionReach startNotifier];
}
-(void)reachabilityChanged:(NSNotification*)note
{
    Reachability * reach = [note object];
    
    if(reach == internetConnectionReach)
    {
        NSString *statusOld = [NSString stringWithFormat:@"%ld",(long)self.oldStatus];
        NSString *statusNew = [NSString stringWithFormat:@"%ld",(long)reach.currentReachabilityStatus];
        self.oldStatus = reach.currentReachabilityStatus;
        
        [[NSNotificationCenter defaultCenter]postNotificationName:NetConnectNotifi object:@{@"old":statusOld,@"new":statusNew}];
        
        
    }
    
}
- (NetworkStatus)currentStatus
{
    return [Reachability reachabilityForInternetConnection].currentReachabilityStatus;
}


@end
